﻿//
// Copyright (c) 2024 BPer
//
#include <bperrtc/BPerStreamUrl.h>

#include <bperutil/sys/BPerLog.h>
#include <bperutil/sys/BPerCUrl.h>
#include <bperutil/sys/BPerMath.h>

int32_t bPer_stream_parseUrl2(char* url,BPerStreamConfig* stream,BPerAVInfo* avinfo,BPerRtcDirection opt){
	int32_t err=BPer_Ok;
	BPerUrlData data;
	bPer_memset(&data,0,sizeof(BPerUrlData));

    if((err=bPer_http_url_parse(avinfo->sys.familyType,url,&data))!=BPer_Ok){
		return bPer_error_wrap(err,"stream parse url error");
	}
    if(opt==BPerSendonly){
    	bPer_memset(avinfo->sys.whipUrl,0,sizeof(avinfo->sys.whipUrl));
    	bPer_strcpy(avinfo->sys.whipUrl,data.stream);
    }else if(opt==BPerRecvonly){
    	bPer_memset(avinfo->sys.whepUrl,0,sizeof(avinfo->sys.whepUrl));
    	bPer_strcpy(avinfo->sys.whepUrl,data.stream);
    }

    bPer_trace("\navinfo->sys.whipUrl==%s",avinfo->sys.whipUrl);

	stream->localPort = 10000+bPer_random()%15000;
	bPer_strcpy(stream->remoteIp,data.server);
	stream->remotePort = data.port;
	//bPer_strcpy(stream->app,data.app);
	//bPer_strcpy(stream->stream,data.stream);
	stream->uid = 0;
	stream->direction = opt;//BPerRecvonly;



return err;

}

int32_t bPer_stream_parseUrl(char* url,BPerStreamConfig* stream,BPerAVInfo* avinfo,BPerRtcDirection opt){
	int32_t err=BPer_Ok;
	BPerUrlData data;
	bPer_memset(&data,0,sizeof(BPerUrlData));

    if((err=bPer_url_parse(avinfo->sys.familyType,url,&data))!=BPer_Ok){
		return bPer_error_wrap(err,"stream parse url error");
	}

	stream->localPort = 10000+bPer_random()%15000;
	bPer_strcpy(stream->remoteIp,data.server);
	stream->remotePort = data.port;
	bPer_strcpy(stream->app,data.app);
	bPer_strcpy(stream->stream,data.stream);
	stream->uid = 0;
	stream->direction = opt;//BPerRecvonly;



return err;

}
